package com.luoyi.util.ehr;


import cn.hutool.core.io.resource.ResourceUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import java.nio.charset.Charset;
import java.util.HashMap;
import java.util.Map;

/**
 * 字符串解析
 *
 * @author xt
 */
public class JsonUnCode {

    /**
     * 数据库18表中的 CLUSTD  【数据】
     */
    private final static String tableDataStr = ResourceUtil.readStr("./text/JsonUnCode/Data.json", Charset.defaultCharset());

    /**
     * 数据库18表中的 CLUSTR   【字段名】
     */
    private final static String headFieldStr = ResourceUtil.readStr("./text/JsonUnCode/HeadField.json", Charset.defaultCharset());

    public static void main(String[] args) {
        execute();
    }

    private static void execute() {
        JSONArray headFields = JSON.parseArray(headFieldStr);
        JSONArray dataArray = JSON.parseArray(tableDataStr);

        // 准备表头数据
        Map<String, JSONObject> headFieldMap = new HashMap<>();
        for (int i = 0; i < headFields.size(); i++) {
            JSONObject item = headFields.getJSONObject(i);
            String infoType = item.getString("INFTY");
            String infoTypeName = item.getString("INFTX");
            JSONArray fields = item.getJSONArray("FIELD");
            for (int j = 0; j < fields.size(); j++) {
                JSONObject field = fields.getJSONObject(j);
                field.put("INFTY_NAME", infoTypeName);
                headFieldMap.put(infoType + "--" + field.getString("FIELD"), field);
            }
        }



        Map<String, JSONObject> resultMap = new HashMap<>(16);

        for (int i = 0; i < dataArray.size(); i++) {
            JSONObject jsonObject = dataArray.getJSONObject(i);
            String infoType = jsonObject.getString("INFTY");
            JSONArray infoTypeItemArray = jsonObject.getJSONArray("VALUE");
            for (int j = 0; j < infoTypeItemArray.size(); j++) {
                JSONObject valueItem = infoTypeItemArray.getJSONObject(j);
                valueItem.forEach((key, value) -> {
                    String fieldKey = infoType + "--" + key;
                    JSONObject field = headFieldMap.get(fieldKey);
                    String infoTypeName = field.getString("INFTY_NAME");
                    JSONObject simpleInfo = new JSONObject();
                    simpleInfo.put("name", field.getString("FIELN"));
                    simpleInfo.put("FTYPE", field.getString("FTYPE"));
                    if (JSON.isValidObject(value.toString())) {
                        simpleInfo.put("VALUE", JSON.parseObject(value.toString()));
                    } else {
                        simpleInfo.put("VALUE", value);
                    }

                    if (resultMap.containsKey(infoType + "-" + infoTypeName)) {
                        resultMap.get(infoType + "-" + infoTypeName).put(key, simpleInfo);
                    } else {
                        JSONObject infoTypeItem = new JSONObject();
                        infoTypeItem.put(key, simpleInfo);
                        resultMap.put(infoType + "-" + infoTypeName, infoTypeItem);
                    }
                });
            }
        }
        System.out.println(JSON.toJSONString(resultMap));
    }


}
